package com.jinbo.footmessage.order;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.OrderBean;
import com.jinbo.footmessage.httpbean.OrdersBean;
import com.jinbo.footmessage.httpretrofit.BusinessController;
import com.jinbo.footmessage.httpretrofit.HttpProtocolFactory;
import com.jinbo.footmessage.httpretrofit.Listener;
import com.jinbo.footmessage.serverInf.ConfirmOrderActivity;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by jinbo on 2015/8/19.
 */
public class NotConfirmItemListViewAdapter extends BaseAdapter {
    List<OrderBean> orders;

    static class ViewHolder {
        CircleImageView head;
        TextView name;
        TextView content;

        TextView date;
        TextView time;
        TextView address;

        Button left;
        Button right;
    }

    private BaseActivity context;

    public NotConfirmItemListViewAdapter(BaseActivity context, List<OrderBean> datalist) {//ArrayList<OrderBean> datalist) {
        this.context = context;
        orders=datalist;
    }

    void setorders( List<OrderBean> datalist)
    {
        this.orders=datalist;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub*//*
        if (orders != null) {
            return orders.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if (orders != null) {
            return orders.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override

    public View getView(final int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_not_confirm_item, null);
            holder = new ViewHolder();

            holder.head=(CircleImageView)convertView.findViewById(R.id.head);
            holder.name=(TextView)convertView.findViewById(R.id.worker_name);
            holder.content=(TextView)convertView.findViewById(R.id.content);
            holder.date=(TextView)convertView.findViewById(R.id.date);
            holder.address=(TextView)convertView.findViewById(R.id.address);
            holder.time=(TextView)convertView.findViewById(R.id.time);
            holder.left=(Button)convertView.findViewById(R.id.left);
            holder.right=(Button)convertView.findViewById(R.id.right);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context)
                .load(HttpProtocolFactory.HOST_URL_USER+orders.get(position).worker.head)
                        // .placeholder(R.mipmap.ic_launcher)
                        // .crossFade()
                .into(holder.head);
        holder.name.setText(orders.get(position).worker.name);
        holder.content.setText(orders.get(position).content);
        holder.date.setText(orders.get(position).date);
        holder.address.setText(orders.get(position).contact_address);
        holder.time.setText(orders.get(position).begin_time + "-"+orders.get(position).end_time);
        holder.right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessController.getInstance().CancelOrder(orders.get(position).id, new Listener<OrdersBean>() {
                    @Override
                    public void onStart(Object... params) {
                        context.showProgressDialog("请稍后...");
                        //mLoadView.setVisibility(View.VISIBLE);
                        //mPullRefreshLayout.setRefreshing(true);
                        // mLoadView.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onComplete(OrdersBean result, Object... params) {

                        context.dismissProgressDialog();

                        // mPullRefreshLayout.setRefreshing(false);
                        //  mLoadView.setVisibility(View.GONE);
                        //  mCallBacks1.change(result.orders);
                        // mCallBacks2.change(result.orders);
                        context.show("撤销成功");
                        orders.remove(position);
                        setorders(orders);

                    }

                    @Override
                    public void onFail(final String msg, Object... params) {
                        //.setVisibility(View.VISIBLE);
                        context.dismissProgressDialog();
                        //mLoadView.setVisibility(View.GONE);
                        // mPullRefreshLayout.setRefreshing(false);
                        if (TextUtils.isEmpty(msg)) {
                        } else {
                            context.show(msg);
                        }
                    }
                });
            }
        });
        holder.left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               LookOrderActivity.launch(context, orders.get(position));
            }
        });
        return convertView;
    }
}
